package edu.worldskills.b;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.CustomViewHolder> {
    private List<Item> items;
    private AdapterView.OnItemClickListener listener;

    // 构造函数
    public CustomAdapter(List<Item> items) {
        this.items = items;
        this.listener = listener;
    }

    // 创建新的 ViewHolder
    @Override
    public CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.img_item, parent, false);
        return new CustomViewHolder(itemView);
    }

    // 绑定数据到 ViewHolder
    @Override
    public void onBindViewHolder(@NonNull CustomViewHolder holder, int position) {
        Item item = items.get(position);
        holder.textView.setText(item.name);
        holder.imageView.setImageBitmap(item.bitmap);

        // 设置 imageView 的单击事件
        holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyApp.getInstance().bitmap=item.bitmap;
                Intent intent=new Intent(v.getContext(),ShowPicActivity.class);
                v.getContext().startActivity(intent);
            }
        });
    }


    @Override
    public int getItemCount() {
        return items.size();
    }

    /// 提供对视图的引用（自定义的ViewHolder）
    public static class CustomViewHolder extends RecyclerView.ViewHolder {
        // 声明你的视图组件引用
        public TextView textView;
        public ImageView imageView;

        // 构造方法中传入整个列表项的视图
        public CustomViewHolder(View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.imageView);
            textView = itemView.findViewById(R.id.textView);
        }
    }
}